import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AlertUtil {
  //中间单个按钮提示
  static atcionShow(BuildContext context, String title,
      {String content = "",
      String confirmTitle = "确认",
      Color confirmColor = Colors.blue,
      Function? handle}) {
    return showDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(title), // 标题组件
            content: content.isNotEmpty ? Text(content) : null, // 内容组件
            // 按钮组件 List
            actions: [
              TextButton(
                onPressed: () {
                  if (handle != null) handle();

                  Navigator.pop(context);
                },
                child: Text(confirmTitle,
                    style: TextStyle(
                      color: confirmColor,
                      fontSize: 18,
                    )),
              ),
            ],
          );
        });
  }

  //传入字符串数组
  static actionListShow(
      BuildContext context, List<String> titles, Function(int) handle,
      {String title = ""}) {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) {
        return CupertinoActionSheet(
          title: title.isNotEmpty ? Text(title) : null,
          actions: getActions(context, titles, handle),
          cancelButton: CupertinoButton(
            child: const Text(
              "取消",
              style: TextStyle(color: Colors.red),
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        );
      },
    );
  }

  //只做两个按钮提示
  static atcionsShow(
    BuildContext context,
    String title, {
    String? content,
    String cancelTitle = "取消",
    String confirmTitle = "确认",
    Color cancelColor = Colors.red,
    Color confirmColor = Colors.black,
    Function? cancelHandle,
    Function? confirmHandle,
  }) {
    return showDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(title), // 标题组件
            content: content != null ? Text(content) : null, // 内容组件
            // 按钮组件 List
            actions: [
              TextButton(
                onPressed: () {
                  if (cancelHandle != null) cancelHandle();
                  Navigator.pop(context);
                },
                child: Text(cancelTitle,
                    style: TextStyle(
                      color: cancelColor,
                      fontSize: 16,
                    )),
              ),
              TextButton(
                onPressed: () {
                  if (confirmHandle != null) {
                    confirmHandle();
                  } else {
                    Navigator.pop(context);
                  }
                },
                child: Text(confirmTitle,
                    style: TextStyle(
                      color: confirmColor,
                      fontSize: 16,
                    )),
              ),
            ],
          );
        });
  }

  static getActions(
      BuildContext context, List<String> titles, Function(int) handle) {
    List<Widget> list = [];
    for (int i = 0; i < titles.length; i++) {
      var tem = CupertinoActionSheetAction(
          onPressed: () {
            Navigator.pop(context);
            handle(i);
          },
          child: Text(titles[i]));
      list.add(tem);
    }
    return list;
  }
}
